package com.ruoyi.web.controller.app;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.req.AppUpdateDTO;
import com.ruoyi.system.service.resp.AppUpdateRespDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: ruohao
 * @Date: 2023/10/30
 **/
@RestController
@RequestMapping("/app/update")
public class AppUpdateController extends BaseController {
    @Autowired
    private RedisCache redisCache;

    private static final String APP_VERSION_KEY = "app_version";
    private static final String APP_WGT_URL_KEY = "app_wgt_url";
    private static final String APP_PKG_URL_KEY = "app_pkg_url";
    private static final String APP_NOTE_KEY = "app_update_note";

    @PostMapping("/getLatest")
    public AjaxResult getLatest(@RequestBody AppUpdateDTO appUpdateDTO) {
        logger.info("获取更新信息 {}", JSON.toJSONString(appUpdateDTO));
        JSONObject latestAppVersion = (JSONObject) redisCache.getCacheObject(APP_VERSION_KEY);
        logger.info("获取新版本号 {}", JSON.toJSONString(latestAppVersion));
        if (StringUtils.isBlank(latestAppVersion.getString(APP_VERSION_KEY))) {
            return success(AppUpdateRespDTO.builder().status(0).build());
        }
        if (!isNewVersion(latestAppVersion.getString(APP_VERSION_KEY), appUpdateDTO.getVersion())) {
            return success(AppUpdateRespDTO.builder().status(0).build());
        }
        JSONObject pkgUrl = (JSONObject) redisCache.getCacheObject(APP_PKG_URL_KEY);
        logger.info("获取下载连接 {}", JSON.toJSONString(pkgUrl));
        JSONObject note = (JSONObject) redisCache.getCacheObject(APP_NOTE_KEY);
        logger.info("获取更新信息 {}", JSON.toJSONString(note));
        AppUpdateRespDTO respDTO = new AppUpdateRespDTO();
        respDTO.setStatus(1);
        respDTO.setNote(note.getString(APP_NOTE_KEY));
        respDTO.setUrl(pkgUrl.getString(APP_PKG_URL_KEY));
        logger.info("获取更新信息接口返回 {}", JSON.toJSONString(respDTO));
        return success(respDTO);
    }

    private Boolean isNewVersion(String newVersion, String oldVersion) {
        String[] oldVersionStrings = oldVersion.split("\\.");
        Integer[] oldVersions = new Integer[3];
        oldVersions[0] = Integer.valueOf(oldVersionStrings[0]);
        oldVersions[1] = Integer.valueOf(oldVersionStrings[1]);
        if (oldVersionStrings.length == 2) {
            oldVersions[0] = 0;
            oldVersions[2] = 0;
        } else {
            oldVersions[2] = Integer.valueOf(oldVersionStrings[2]);
        }

        String[] newVersionStrings = newVersion.split("\\.");
        Integer[] newVersions = new Integer[3];
        newVersions[0] = Integer.valueOf(newVersionStrings[0]);
        newVersions[1] = Integer.valueOf(newVersionStrings[1]);
        newVersions[2] = Integer.valueOf(newVersionStrings[2]);
        if (oldVersions[0] > newVersions[0]) {
            return false;
        }
        if (oldVersions[0] < newVersions[0]) {
            return true;
        }
        if (oldVersions[1] > newVersions[1]) {
            return false;
        }
        if (oldVersions[1] < newVersions[1]) {
            return true;
        }
        if (oldVersions[2] > newVersions[2]) {
            return false;
        }
        if (oldVersions[2] < newVersions[2]) {
            return true;
        }
        return false;
    }
}
